define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index',
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'ordersn', title: __('Ordersn'),operate:'LIKE'},
                        {field: 'member.nickname', title: __('购买者昵称'),operate:'LIKE'},
                        {field: 'agentid', title: __('Agentid')},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'prepay', title: __('Prepay'), operate:false},
                        {field: 'residue', title: __('Residue'), operate:false},
                        {field: 'discount.title', title: __('Discount.title'),operate:false},
                        // {field: 'discount_id', title: __('Discount_id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'),  operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'id', title: __('Id')},
                        // {field: 'openid', title: __('Openid')},
                        // {field: 'ordersn', title: __('Ordersn')},
                        // {field: 'agentid', title: __('Agentid')},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'discount_id', title: __('Discount_id')},
                        // {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        // {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        // {field: 'statis', title: __('Statis'), searchList: {"0":__('Statis 0'),"1":__('Statis 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.id', title: __('Member.id')},
                        // {field: 'member.openid', title: __('Member.openid')},
                        // {field: 'member.avatar_image', title: __('Member.avatar_image'), formatter: Table.api.formatter.image},
                        // {field: 'member.username', title: __('Member.username')},
                        // {field: 'member.nickname', title: __('Member.nickname')},
                        // {field: 'member.mobile', title: __('Member.mobile')},
                        // {field: 'member.agentid', title: __('Member.agentid')},
                        // {field: 'member.identity', title: __('Member.identity')},
                        // {field: 'member.membersn', title: __('Member.membersn')},
                        // {field: 'member.p_id', title: __('Member.p_id')},
                        // {field: 'member.credit', title: __('Member.credit'), operate:'BETWEEN'},
                        // {field: 'member.createtime', title: __('Member.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'discount.id', title: __('Discount.id')},
                        // {field: 'discount.title', title: __('Discount.title')},
                        // {field: 'discount.full', title: __('Discount.full'), operate:'BETWEEN'},
                        // {field: 'discount.money', title: __('Discount.money'), operate:'BETWEEN'},
                        // {field: 'discount.endtime', title: __('Discount.endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'discount.createtime', title: __('Discount.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var list = Config.list;
            var disruleList = list.disruleList;
            var gTypeList = list.gTypeList;
            var recordNumber = 0;

            //搜索会员
            $('.serachMember').click(function () {
                var memberNum = $('#c-memberNum').val();
                if(memberNum == ''){
                    layer.alert('会员编号不能为空');
                    return false;
                }
                $.ajax({
                    url:'/admin/order/order/serachMember',
                    method:'GET',
                    data:{'memberNum':memberNum},
                    success:function (res) {
                        if(res.code == 1){
                            $('.memberItem').remove();
                            var member = res.data.member;
                            var agentInfo = res.data.agentInfo;
                            var html = '<div class="memberItem">';
                            //拼接用户信息
                            html +='<div class="form-group"><label class="control-label col-xs-12 col-sm-2">用户信息</label><div class="col-xs-12 col-sm-8"><table class="table table-bordered"><tr><td>昵称</td><td>电话</td><td>身份</td><td>积分</td></tr>';
                            html += "<tr><td>" + member.nickname;
                            html += '</td><td>' + member.mobile + '</td>'
                            html += '<td>' + member.identityname + '</td><td>' + member.credit
                            html += ' </td></tr>'
                            html +=  '</table></div></div>'
                            //拼接上线
                            html +='<div class="form-group online"><label class="control-label col-xs-12 col-sm-2">上线信息</label><div class="col-xs-12 col-sm-8"><table class="table table-bordered"><tr><td>昵称</td><td>电话</td><td>身份</td><td>积分</td></tr>';

                            if(agentInfo.length<1){
                                html += '<tr><td colspan="5" style="text-align: center">暂无上线</td></tr>'
                            }else{
                                html += "<tr><td>" + agentInfo.nickname;
                                html += '</td><td>' + agentInfo.mobile + '</td>'
                                html += '<td>' + agentInfo.identityname + '</td><td>' + agentInfo.credit
                                html += ' </td></tr>'
                            }

                            html +=  '</table></div></div>';
                            html +=  '</div>';

                            $('.serachMember').parents('.member').after(html);
                        }else{
                            $('#c-memberNum').val('');
                            layer.alert(res.msg)
                        }
                    }
                })
            });

            // 点击增加商品模块
            $(document).on('click','.addGoodBtn',function () {
                recordNumber = recordNumber + 1;
                var html = '';
                //外边框
                html += '<div class="goodItem row" style="margin-bottom: 10px;"><label class="control-label col-xs-12 col-sm-2">填写购买商品</label>';
                html +=  '<div class="col-xs-12 col-sm-8"><div class="row">'
                //类型选择
                html += '<div class="span6 col-xs-12 col-sm-2"><select name="row[type][]" id="type" class="form-control"><option>选择类型</option>'
                for (var i=0;i<gTypeList.length;i++){
                    html +=  '<option value="'+gTypeList[i].id+'">'+gTypeList[i].title+'</option>'
                }
                html += '</select></div>'
                //名称
                html +=  '<div class="span6 col-xs-12 col-sm-3"><input class="form-control" name="row[title][]" type="text" placeholder="输入商品名称"></div>'
                //金额
                html +=  '<div class="span6 col-xs-12 col-sm-2"><input class="form-control" name="row[money][]" type="text" placeholder="输入金额"></div>'
                //颜色
                html += '<div class="span6 col-xs-12 col-sm-5">'
                for (var j=0;j<disruleList.length;j++){
                    html +=  '<label class="radio-inline">';
                    html += '<input type="radio" name="row[color]['+ recordNumber +']"  value="'+ disruleList[j].id +'" checked >';
                    html += '<span style="display:inline-block;width: 10px;height: 10px;background-color:'+disruleList[j].color + '"></span>'
                    html += '</label>'
                }
                html += '</div>'
                //下面的两个按钮和外边框结束
                html += '</div></div>'
                html += '<div class="col-xs-12 col-sm-1"><button class="btn btn-info addGoodBtn" type="button">增加</button></div>';
                html += '<div class="col-xs-12 col-sm-1"><button class="btn btn-danger delGoodBtn" type="button">删除</button></div>';
                html += '</div>';
                $(this).parents('.goodItem').after(html);
            })
            //清除模块
            $(document).on('click','.delGoodBtn',function () {
                $(this).parents('.goodItem').remove();
            })

            //搜索优惠券
            $('.serachDiscount').on('click',function () {
                var discountNum = $('#c-discountNum').val();
                if(discountNum == ''){
                    layer.alert('优惠券码不能为空');
                    return false;
                }
                $.ajax({
                    url:'/admin/order/order/serachDiscount',
                    method:'GET',
                    data:{'discount':discountNum},
                    success:function (res) {
                        if(res.code == 1){
                            var discount = res.data;
                            $('.discountItem').remove();
                            var html = '<div class="discountItem">';
                            html += '<div class="form-group"><label class="control-label col-xs-12 col-sm-2">详情</label>';
                            html += '<div class="col-xs-12 col-sm-8"><table class="table table-bordered">';
                            html += '<tr><td>' +discount.discount.title + '</td><td>满' + discount.discount.full + '元</td><td>送' + discount.discount.money +'元</table></div>';
                            html += '</table></div>'
                            html += ' </div></div>';
                            $('.serachDiscount').parents('.discount').after(html);
                        }else{
                            $('.discountItem').remove();
                            $('#c-discountNum').val('');
                            layer.alert(res.msg);
                        }
                    }
                })

            })

            //搜索积分
            $('.serachCode').on('click',function () {
                var code = $('#c-code').val();
                if(code == ''){
                    layer.alert('积分码不能为空');
                    return false;
                }

                $.ajax({
                    url:'/admin/order/order/searchCode',
                    method:'GET',
                    data:{'code':code},
                    success:function (res) {
                        if(res.code == 1){
                            var code = res.data;
                            $('.codeItem').remove();
                            var html = '<div class="codeItem">';
                            html += '<div class="form-group"><label class="control-label col-xs-12 col-sm-2">金额</label>';
                            html += '<div class="col-xs-12 col-sm-8"><table class="table table-bordered">';
                            html += '<tr><td>' +code.nickname + '</td><td>积分:' + code.credit + '分</td><td>可抵用金额:' + code.amount +'元</table></div>';
                            html += '</table></div>'
                            html += ' </div></div>';
                            $('.serachCode').parents('.code').after(html);
                        }else{
                            $('.codeItem').remove();
                            $('#c-code').val('');
                            layer.alert(res.msg);
                        }
                    }
                })

            })


            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});